package server;

import com.alibaba.fastjson.JSONObject;
import com.youpai.platform.chat.web.entity.dto.ClientC2CMsgDTO;
import com.youpai.platform.vulture.common.entity.AccessMessage;
import com.youpai.platform.vulture.common.entity.Header;
import com.youpai.platform.vulture.common.enums.ClientCommandEnum;
import com.youpai.platform.vulture.common.enums.HeaderTypeEnum;
import com.youpai.platform.vulture.common.enums.MsgFlagEnum;
import com.youpai.platform.vulture.common.handler.CustomMessageDecoder;
import com.youpai.platform.vulture.common.handler.CustomMessageEncoder;
import com.youpai.platform.vulture.service.entity.dto.HandShakeDTO;
import com.youpai.platform.vulture.service.entity.dto.C2CMessageDTO;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.assertj.core.util.Lists;
import org.junit.Test;

import java.util.Scanner;

/**
 * @date: 2022/6/23 14:46
 * @author: Anna
 * @version: 1.0
 * @description:
 */
public class TcpClientTest2 {

    @Test
    public void testTcpClient() {
        Bootstrap bootstrap = new Bootstrap();
        Channel channel = null;
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        bootstrap.channel(NioSocketChannel.class)
                .group(workerGroup)
                .option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new CustomMessageEncoder()).addLast(new CustomMessageDecoder())
                                .addLast(new NettyServerHandler2());
                    }
                });
        try {
            ChannelFuture channelFuture = bootstrap.connect("localhost", 1001).sync();
            channel = channelFuture.channel();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (true) {
            Scanner sc = new Scanner(System.in);
            int i = sc.nextInt();
            AccessMessage accessMessage = new AccessMessage();
            if (i == 0) {
                accessMessage.setFlag(MsgFlagEnum.HEART_BEAT_PONG.flag);
                channel.writeAndFlush(accessMessage);
            }
            if (i == 1) {
                accessMessage.setFlag(MsgFlagEnum.HEART_BEAT_PING.flag);
                channel.writeAndFlush(accessMessage);
            } else if (i == 2) {
                accessMessage.setFlag(MsgFlagEnum.HAND_SHAKE.flag);
                HandShakeDTO handShakeDTO = new HandShakeDTO();
                handShakeDTO.setAccId("1_710862898493923328")
                        .setAppId(1)
                        .setCVer("10.10.10")
                        .setVVer("11.11.11")
                        .setDId("55555")
                        .setImType(1)
                        .setVultureToken("710488484321435648");
                accessMessage.setBody(JSONObject.toJSONString(handShakeDTO));
                channel.writeAndFlush(accessMessage);
            } else if (i == 3) {
                accessMessage.setFlag(MsgFlagEnum.ORDINARY_MSG.flag);
                Header header1 = new Header();
                header1.setHeaderType(HeaderTypeEnum.COMMAND.type)
                        .setHeaderValue(ClientCommandEnum.CLI_SEND_MSG.cmd);
                accessMessage.setHeaderList(Lists.list(header1));
                accessMessage.setBody(
                        "{\"content\":\"gyyyyuu\",\"conversationId\":\"1_710863008971890688\",\"fromAccId\":\"1_710862898493923328\",\"toAccId\":\"1_710863008971890688\",\"msgId\":null,\"preMsgId\":null,\"clientMsgId\":\"33568579-8ca3-4031-8cdc-2516654492fa|1_710477535510274048|1659538758688\",\"msgType\":0,\"timeStamp\":1659538758688}");
                channel.writeAndFlush(accessMessage);
            }
        }
    }
}
